package org.vacuela.attendance;

import java.util.List;
import java.util.logging.Level;

import com.u2d.app.User;
import org.hibernate.Query;
import com.u2d.model.AbstractComplexEObject;
import com.u2d.model.AbstractListEO;
import com.u2d.model.Title;
import com.u2d.type.atom.DateEO;
import com.u2d.type.atom.Password;
import com.u2d.type.atom.StringEO;
import com.u2d.type.atom.TextEO;
import com.u2d.type.atom.TimeEO;
import com.u2d.type.composite.LoggedEvent;

import javax.persistence.Entity;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

@Entity
public class Attendance extends AbstractComplexEObject {
	private User employee;
	private final TimeEO timeRecord = new TimeEO();
	private final DateEO dateRecord = DateEO.today();

	public DateEO getDateRecord() {
		return dateRecord;
	}

	private final Branch branch = new Branch();
	private final PunchInOut punchInOut = new PunchInOut();
	private final StringEO userCode = new StringEO();
	private final TextEO notes = new TextEO();

	public TextEO getNotes() {
		return notes;
	}

	public StringEO getUserCode() {
		return userCode;
	}

	public PunchInOut getPunchInOut() {
		return punchInOut;
	}

	public Branch getBranch() {
		return branch;
	}

	public TimeEO getTimeRecord() {
		return timeRecord;
	}

	public static String[] fieldOrder = { "employee", "dateRecord",
			"timeRecord", "branch", "punchInOut", "userCode", "notes" };
	public static String[] readOnly = { "employee", "dateRecord", "timeRecord" };

	public User getEmployee() {
		return employee;
	}

	public void setEmployee(User employee) {
		User oldValue = this.employee;
		this.employee = employee;
		firePropertyChange("employee", oldValue, this.employee);
	}

	public Attendance() {

	}

	public void initialize() {
		employee = currentUser();
		getLastEntry();
	}

	public Title title() {
		return employee.title().appendSpace().appendBracket(
				punchInOut.caption()).appendSpace().appendBracket(
				timeRecord.toString()).appendSpace().appendBracket(
				branch.caption());
	}

	public int validateCui() {
		/*
		 * String queryString =
		 * "from User u where username = :username and u.cui = :cui "; Query
		 * hqlQuery = hbmPersistor().getSession().createQuery(queryString);
		 * hqlQuery.setParameter("username", currentUser().getUsername());
		 * hqlQuery.setParameter("cui", getUserCode().stringValue());
		 * AbstractListEO list = hbmPersistor().hqlQuery(hqlQuery); if (list ==
		 * null) { tracer().log(Level.INFO, "List equal null"); } else { List
		 * sessions = list.getItems(); tracer().log(Level.INFO, "sessions => " +
		 * sessions.size()); for (Object obj : sessions) {
		 * tracer().log(Level.INFO, "" + obj); } }
		 */
		String inputCode = getUserCode().stringValue();
		if (currentUser().getCui().stringValue().equals(inputCode)) {
			tracer().log(Level.INFO, "Validation  OK :: User Authenticated");
			return 0;
		} else {
			tracer().log(Level.INFO, "Validation FAIL :: User not idenitified");
			fireValidationException("The User Code is not recognized");
			return 1;
		}
	}

	private void getLastEntry() {
		/*
		 * String queryString =
		 * "select user.password from com.u2d.app.User as user " +
		 * " where user.username = :username"; Query query =
		 * hbmPersistor().getSession().createQuery(queryString);
		 * query.setString("username", username); Password hash = (Password)
		 * query.uniqueResult();
		 */
		String queryString = "from Attendance a where a.employee = :employee and a.dateRecord = today() order by a.timeRecord desc";
		Query hqlQuery = hbmPersistor().getSession().createQuery(queryString);
		hqlQuery.setParameter("employee", currentUser());
		AbstractListEO list = hbmPersistor().hqlQuery(hqlQuery);
		if (list == null || list.isEmpty()) {
			tracer().log(Level.INFO, "List equal null");
			this.punchInOut.setValue(PunchInOut.PUNCH_IN);
		} else {
			List sessions = list.getItems();
			tracer().log(Level.INFO, "sessions => " + sessions.size());
			for (Object obj : sessions) {
				tracer().log(Level.INFO, "" + obj);
			}
		}
	}

	public int validate() {
		int totalErrors = validateCui();

		return totalErrors;
	}

}
